package ebuy.com.cotroller;

import ebuy.com.pojo.Easybuy_order;
import ebuy.com.pojo.Easybuy_order_detail;
import ebuy.com.pojo.Easybuy_user;
import ebuy.com.pojo.Shopping;
import ebuy.com.server.Commodityserver;
import ebuy.com.server.imp.Easybus_orderserverimp;
import ebuy.com.server.imp.Easybuy_order_detailsserverimp;
import ebuy.com.server.imp.Easybuy_userserverimp;
import ebuy.com.server.imp.Shoppingserverimp;
import ebuy.com.util.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class OrderCotroller {
    @Autowired
    private Commodityserver commodityserver;
    @Autowired
    private Easybuy_order_detailsserverimp easybuy_order_detailsserverimp;
    @Autowired
    private Easybus_orderserverimp easybus_orderserverimp;
    @Autowired
    private Easybuy_userserverimp easybuy_userserverimp;
    @Autowired
    private Shoppingserverimp shoppingserverimp;

    @GetMapping("/getorder")
    public RespBean GetOrder(String s, String usename) {
        System.out.println(s);
        String[] str = s.split(",");

        Easybuy_order easybuy_order = new Easybuy_order();
        //订单表  查询出来  总钱
        String string = UUID.randomUUID().toString();
        //查询默认地址
        Easybuy_user lookfortheaddress = easybuy_userserverimp.lookfortheaddress(usename,1);
        double integer = commodityserver.Checkthebalanceoftheshoppingcart(usename);
        easybuy_order.setCost(integer);//总钱
        easybuy_order.setSerialNumber(string);//订单号
        easybuy_order.setUserAddress(lookfortheaddress.getAddress());//地址
        easybuy_order.setLoginName(usename);//用户名字
        Integer querybyusername = easybuy_userserverimp.Querybyusername(usename);//根据名字查询出用户id
        easybuy_order.setUserId(querybyusername);
        int increaseorder = easybus_orderserverimp.Increaseorder(easybuy_order);
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
            Shopping i1 = shoppingserverimp.Queryshoppingcartid(Integer.parseInt(str[i]));//查询出商品的id
            Integer checkthepriceoftheproduct = commodityserver.Checkthepriceoftheproduct(Integer.parseInt(str[i]), usename);
            //商品id
            Easybuy_order_detail easybuy_order_detail = new Easybuy_order_detail();
            easybuy_order_detail.setOrderId(easybuy_order.getId());//订单id
            easybuy_order_detail.setCost(checkthepriceoftheproduct);//商品的价格
            easybuy_order_detail.setProductId(i1.getCommoditysid());//商品的主键
            easybuy_order_detail.setQuantity(i1.getNumberofgoods());//商品的数量
            int connectordertoincrease = easybuy_order_detailsserverimp.Connectordertoincrease(easybuy_order_detail);
            if (increaseorder==1&connectordertoincrease==1){
                //进行删除订单
                int i2 = shoppingserverimp.Deleteshoppingcart(Integer.parseInt(str[i]));
            }
        }


        return RespBean.success("查询成功",string);
    }

}
